<?php 

namespace Home\Controller;
use Think\Controller;
class PublishController extends CommonController {
	public function homepage()
	{	

		$id = I('get.id');
		$this->autoAdd('question','view_num',"+1",$id);

		//问题数据
		$arr = "select t1.*,t2.nickname,t2.face from question t1 inner join user t2 on t1.uid=t2.id where t1.id='{$id}'";
        //问题的资料
		$res = M()->query($arr);
		$res = current($res);
        $this->assign('title',$res['title']);
		$this->assign('res',$res);
        // $res = M('user')->where(array('id'=>$id))->find();
        // 热门浏览
		$this->getTopview();
        //热门回复
		$this->getTopanswer();
        //回复问题的数据
		$sql = "select t1.*,t2.nickname,t2.face from answer t1 inner join user t2 on t1.uid=t2.id  where t1.qid ='{$id}' order by create_time desc";
		
		$answer = M()->query($sql);

		$this->assign('answer',$answer);
       
    	
		$this->display();
	}
    public function index(){

        if ($_SESSION['id']) {
                    //分类
        $arr = D('category')->getCate();
        // var_dump($arr);
        $this->assign('arr',$arr);

        //人类验证
         $question = D('verify')->Oneverify();

        $this->assign('question',$question);
        $this->display();
        }else{
            $this->error('未登陆，请先登陆',U('home/login/index'));
        }

    }
    public function checkdata()
    {
    	$verify = I('post.verify');
        // closelog($verify);
        // exit;
        if ($verify!=$_SESSION['answer']) {
            $arr = array(
                'error' => 1,
                'info' => '人类验证不通过'
                );
            echo json_encode($arr);
            exit;
        }

        $data = I('post.');

        $data['uid']=$_SESSION['id'];
        $data['create_time'] = time();
        $res = M('question')->add($data);
        if ($res) {
             $arr = array(
                'error' => 0,
                'info' => '发布成功',
                'url'=>U('home/Publish/homepage',array('id'=>$res))
                );
            echo json_encode($arr);
        }else{
             $arr = array(
                'error' => 1,
                'info' => '发布失败'
                );
            echo json_encode($arr);
        }


    }
    public function img()
    {
    	 $upload = new \Think\Upload();// 实例化上传类
    $upload->maxSize   =     3145728 ;// 设置附件上传大小
    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
    $upload->rootPath  =     './Img/'; // 设置附件上传根目录
    $upload->savePath  =     ''; // 设置附件上传（子）目录
    // 上传文件 
    $info   =   $upload->upload();
    // var_dump($info);
    if(!$info) {// 上传错误提示错误信息
     echo $upload->getError();
    }else{// 上传成功

	    	$info = current($info);

	    	$sql =__ROOT__."/Img/".$info['savepath'].$info['savename'];

	    	
	    	$arr = array(
	    		'code'=>0,
	    		'msg'=>'成功',
	    		'data'=>array(
	    			'src'=>$sql,
	    			'title'=>0
	    			),

	    		);
	    	echo json_encode($arr);
	    	
    }



}
	public function checkdate()
	{

		$id = $_SESSION['id'];
		$content = I('post.content');
		$qid = I('post.qid');
		$data = array(
			'uid'=>$id,
			'content'=>$content,
			'create_time'=>time(),
			'qid'=>$qid,
			);
         if ($_SESSION['id']) {
		  $res = M('answer')->add($data);
            if ($res) {
                $this->autoAdd('question','answer_num',"+1",I('post.qid'));
                $this->autoAdd('user','answer_num',"+1",$id);
                 $arr = array(
                    'error' => 0,
                    'info' => '回复成功'
                    );
                echo json_encode($arr);
            }else{
                 $arr = array(
                    'error' => 1,
                    'info' => '回复失败'
                    );
                echo json_encode($arr);
            };

        }else{
            $arr = array(
                    'error' => 2,
                    'info' => '未登陆，请先登陆',
                    'url'=>U('home/login/index')
                    );
                echo json_encode($arr);
            // $this->error('未登陆，请先登陆');
        }


	}
    public function bj()
    {
        $id = $_GET['id'];

        $sql = "select t1.*,t2.cname from question t1 inner join category t2 on t1.cid=t2.id where t1.id={$id}";

        // var_dump($sql);exit;

        $sql = M()->query($sql);
        $sql = current($sql);
        $this->assign('sql',$sql);
         $arr = D('category')->getCate();
        // var_dump($arr);
        $this->assign('arr',$arr);

        $question = D('verify')->Oneverify();

        $this->assign('question',$question);
        // var_dump($_GET);
        $this->display();
    }
    public function check()
    {   

        $res = I('post.');

        $verify = I('post.verify');

        $row = I('post.qid');
        // var_dump($row);
        // exit;
        // var_dump($res);
        if ($verify!=$_SESSION['answer']) {
             $arr = array(
                    'error' => 1,
                    'info' => '人类验证不通过'
                    );
                echo json_encode($arr);
                exit;
        }
        $arr = M('question')->where(array('id'=>$row))->save($res);
        if ($arr) {
            $arr = array(
                'error' => 0,
                'info' => '更改成功',
                'url'=>U('home/Publish/homepage',array('id'=>$row))
                );
            echo json_encode($arr);
            exit;
        }else{
             $arr = array(
                'error' => 1,
                'info' => '更改失败'
                );
            echo json_encode($arr);
        }
        
        
    }

}
?>